package com.miaoxie.forum.module.monitor.server.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ai.Base64Util;
import com.baidu.ai.GetTextByImg;
import com.miaoxie.forum.module.auth.shared.controller.ResponseMessageController;
import com.miaoxie.forum.module.auth.shared.json.JSON;
import com.miaoxie.forum.module.auth.shared.security.SecurityNonCheckRequired;
import com.miaoxie.forum.module.auth.shared.utils.HttpClientUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: miaoxie
 * Date: 2018-03-20
 * Time: 上午9:45
 */
@Controller
@RequestMapping("/miaoxie/gaode/map")
@SecurityNonCheckRequired
public class GaodeMapController extends ResponseMessageController {
    @RequestMapping("/getlonlat")
    @ResponseBody
    @JSON
    public Map<String, Object> getBaiduOcr(String city) throws IOException {

        String url = "https://restapi.amap.com/v3/config/district?subdistrict=0&extensions=all&level=district&key=04f224f36ed45528fb3e414e04789003&s=rsv3&output=json&keywords="+city+"&callback=jsonp_646376_&platform=JS&logversion=2.0&appname=http%3A%2F%2Flocalhost%3A63342%2Fforum%2Fforum-web%2Ftest.html%3F_ijt%3Dtgi2nkr285v0iik9c9fp4g6tt2&csid=8328A3F7-B145-4D6F-B79E-752298C9367B&sdkversion=1.4.8";
        String resultStr = HttpClientUtil.sendHttpGet(url);

        resultStr = resultStr.split("\\(")[1];
        resultStr = resultStr.split("\\)")[0];


        JSONObject jsonObject = JSONObject.parseObject(resultStr);
        JSONArray jsonArray = (JSONArray) jsonObject.get("districts");
        Map<String,Object> map = (Map<String, Object>) jsonArray.get(0);
        String str = (String) map.get("polyline");

        List<String> listK = new ArrayList<>();

        List<String> temp = Arrays.asList(str.split("\\|"));
        for (String stemp : temp){
            listK.add(stemp);
        }


        List<String> tt = new ArrayList<>();


//        for (String s : list){
//            Double d = Double.valueOf(s.split(",")[0]);
//            Double e = Double.valueOf(s.split(",")[1]);
//
//        }

        for (String strTemp : listK){
            List<String> list = Arrays.asList(strTemp.split(";"));
            for (int i=1;i<list.size();i++){
                Double lon1 = Double.valueOf(list.get(i).split(",")[0]);
                Double lat1 = Double.valueOf(list.get(i).split(",")[1]);

                Double lon2 = Double.valueOf(list.get(i-1).split(",")[0]);
                Double lat2 = Double.valueOf(list.get(i-1).split(",")[1]);

                BigDecimal b1 = new BigDecimal(lon1.toString());
                BigDecimal b2 = new BigDecimal(lon2.toString());

                BigDecimal b3 = new BigDecimal(lat1.toString());
                BigDecimal b4 = new BigDecimal(lat2.toString());

                String lonJ = String.valueOf(b1.subtract(b2));
                String lonW = String.valueOf(b3.subtract(b4));

                tt.add(lonJ+","+lonW);

            }
        }

        this.getMap().put("lonlat",Arrays.asList(str.split("\\|")).get(0).split(";"));

        return returnSuccess();
    }

}
